<?php

namespace backend\controllers;

use Yii;
use common\models\links;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\Upload;


/**
 * LinksController implements the CRUD actions for links model.
 */
class LinksController extends AdminController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all links models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => links::find()
        ]);

		Yii::$app->view->params['titles'] = '友情链接';
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Creates a new links model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new links();

        if ($model->load(Yii::$app->request->post())) {

			/* 处理上传的图片 */
			if ($_FILES['Links']['error']['image'] === 0) {
				/* 把文件的路劲赋值给image字段 */
				$model->image = Upload::uploadImage($model, 'image', 'other');
			}

			if ($model->save()) {
				Yii::$app->session->setFlash('success', '新增成功');
			}

            return $this->redirect(['index']);
        } else {
			Yii::$app->view->params['titles'] = '新增链接';
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing links model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
			/* 处理上传的图片 */
			if ($_FILES['Links']['error']['image'] === 0) {
				/* 把文件的路劲赋值给image字段 */
				$model->image = Upload::uploadImage($model, 'image', 'other');
			}

			if ($model->save()) {
				Yii::$app->session->setFlash('success', '编辑成功');
			}
            return $this->redirect(['index']);
        } else {
			Yii::$app->view->params['titles'] = '编辑链接';
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing links model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
		if ($this->findModel($id)->delete()) {
			Yii::$app->session->setFlash('success', '删除成功');
		}

        return $this->redirect(['index']);
    }

	/**
	 * 批量删除提交地址
	 * @return mixed
	 */
	public function actionDelall(){
		if (Yii::$app->request->isPost) {
			if (is_array($_POST['selection'])) {
				$id = implode(',', $_POST['selection']);

				if (Links::deleteAll("id in({$id})")) {
					Yii::$app->session->setFlash('success', '删除成功');
					return $this->redirect(['index']);
				}
			}
		}

		return $this->redirect(['index']);
	}

    /**
     * Finds the links model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return links the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = links::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
